package com.zhao.utils

import org.apache.commons.lang3.StringUtils
import org.apache.spark.sql.Row

/**
 * Description: 标签工具类<br/>
 * Copyright (c) ，2021 ， 赵 <br/>
 * A wet person does not fear the rain. <br/>
 * Date： 2021/1/15 16:11
 *
 * @author 柒柒
 * @version : 1.0
 */

object TagUtils {

  /**
   * 处理数据中所有所有的用户信息,只要能够唯一标识该用户的字段值不为空,就将该字段合并起来,作为用户唯一标识的综合考虑
   * @param row
   * @return
   */
  def getRowAllUserId(row: Row): List[String] = {
    var list = List[String]()
    if (StringUtils.isNotBlank(row.getAs[String]("imei")))
      list :+= "IM:" + row.getAs[String]("imei")
    if (StringUtils.isNotBlank(row.getAs[String]("mac")))
      list :+= "MC:" + row.getAs[String]("mac")
    if (StringUtils.isNotBlank(row.getAs[String]("idfa")))
      list :+= "ID:" + row.getAs[String]("idfa")
    if (StringUtils.isNotBlank(row.getAs[String]("openudid")))
      list :+= "OD:" + row.getAs[String]("openudid")
    if (StringUtils.isNotBlank(row.getAs[String]("androidid")))
      list :+= "AOD:" + row.getAs[String]("androidid")
    if (StringUtils.isNotBlank(row.getAs[String]("imeimd5")))
      list :+= "IMM:" + row.getAs[String]("imeimd5")
    if (StringUtils.isNotBlank(row.getAs[String]("macmd5")))
      list :+= "MCM:" + row.getAs[String]("macmd5")
    if (StringUtils.isNotBlank(row.getAs[String]("idfamd5")))
      list :+= "IDM:" + row.getAs[String]("idfamd5")
    if (StringUtils.isNotBlank(row.getAs[String]("openudidmd5")))
      list :+= "ODM:" + row.getAs[String]("openudidmd5")
    if (StringUtils.isNotBlank(row.getAs[String]("androididmd5")))
      list :+= "AODM:" + row.getAs[String]("androididmd5")
    if (StringUtils.isNotBlank(row.getAs[String]("imeisha1")))
      list :+= "IMS:" + row.getAs[String]("imeisha1")
    if (StringUtils.isNotBlank(row.getAs[String]("macsha1")))
      list :+= "MCS:" + row.getAs[String]("macsha1")
    if (StringUtils.isNotBlank(row.getAs[String]("idfasha1")))
      list :+= "IDS:" + row.getAs[String]("idfasha1")
    if (StringUtils.isNotBlank(row.getAs[String]("openudidsha1")))
      list :+= "ODS:" + row.getAs[String]("openudidsha1")
    if (StringUtils.isNotBlank(row.getAs[String]("androididsha1")))
      list :+= "AODS:" + row.getAs[String]("androididsha1")

    list
  }

  /**
   * 从当前的记录中取出一个第一个不为空的字段值(能够唯一标识用户的),作为用户的userID
   * @return
   */
  def getAnyOneUserID:PartialFunction[Row,String] ={
    case v if StringUtils.isNotBlank(v.getAs[String]("imei")) =>
      "IM"+v.getAs[String]("imei")
    case v if StringUtils.isNotBlank(v.getAs[String]("mac")) =>
      "MC" + v.getAs[String]("mac")
    case v if StringUtils.isNotBlank(v.getAs[String]("idfa")) =>
      "ID" + v.getAs[String]("idfa")
    case v if StringUtils.isNotBlank(v.getAs[String]("openudid")) =>
      "OD" + v.getAs[String]("openudid")
    case v if StringUtils.isNotBlank(v.getAs[String]("androidid")) =>
      "AOD" + v.getAs[String]("androidid")
    case v if StringUtils.isNotBlank(v.getAs[String]("imeimd5")) =>
      "MD5IM" + v.getAs[String]("imeimd5")
    case v if StringUtils.isNotBlank(v.getAs[String]("macmd5")) =>
      "MD5MC" + v.getAs[String]("macmd5")
    case v if StringUtils.isNotBlank(v.getAs[String]("openudidmd5")) =>
      "MD5OD" + v.getAs[String]("openudidmd5")
    case v if StringUtils.isNotBlank(v.getAs[String]("androididmd5")) =>
      "MD5AOD" + v.getAs[String]("androididmd5")
    case v if StringUtils.isNotBlank(v.getAs[String]("idfamd5")) =>
      "MD5ID" + v.getAs[String]("idfamd5")
    case v if StringUtils.isNotBlank(v.getAs[String]("imeisha1")) =>
      "SHIM" + v.getAs[String]("imeisha1")
    case v if StringUtils.isNotBlank(v.getAs[String]("macsha1")) =>
      "SHMC" + v.getAs[String]("macsha1")
    case v if StringUtils.isNotBlank(v.getAs[String]("idfasha1")) =>
      "SHID" + v.getAs[String]("idfasha1")
    case v if StringUtils.isNotBlank(v.getAs[String]("openudidsha1")) =>
      "SHOD" + v.getAs[String]("openudidsha1")
    case v if StringUtils.isNotBlank(v.getAs[String]("androididsha1")) =>
      "SHAOD" + v.getAs[String]("androididsha1")

  }

  /**
   * 在日志文件中众多字段中,下述的每一个字段,都可以作为当前用户的唯一标识
   */
  val hasneedOneUserId =
    "imei !='' " +
      "or mac!='' " +
      "or idfa!='' " +
      "or openudid!='' " +
      "or androidid!='' " +
      "or imeimd5!='' " +
      "or macmd5!= '' " +
      "or openudidmd5!='' " +
      "or idfamd5!='' " +
      "or androididmd5!='' " +
      "or imeisha1!='' " +
      "or macsha1!='' " +
      "or idfasha1!='' " +
      "or openudidsha1!='' " +
      "or androididsha1!='' "
}
